/**
 * @fileOverview *************************
 * 
 * @version 0.0.1_2009-06-26
 * @requires jquery.js
 */
/* ------------------------------------------------------------------------------ */


/* ============================================================================== */

function StyleSwitcher( className, defaultId ){
	if(!className) return;

	this.className = className;

	this.defaultId = defaultId || null;

	this.stylesheets = [];

	this.cookieName = 'style-' + className;

	this.cookieExpires = 365;

	this.init();
}

/* ----------------------------------------------------- */
StyleSwitcher.prototype.init = function(){
	var links = document.getElementsByTagName("LINK");
	if( !links ) return;
	var _this = this;

	for (var i=0; i<links.length; i++) {
		if (links[i].getAttribute("rel") && links[i].getAttribute("rel").match(/stylesheet/i)) {
			if ($(links[i]).hasClass(this.className) && links[i].getAttribute("id")) {
			//if( u.hasClassName(links[i], this.className) && links[i].getAttribute("id") ){
				this.stylesheets.push(links[i]);
			}
		}
	}

	var c = this.readCookie(this.cookieName);
	var id = c? c : (this.defaultId? this.defaultId : null );
	if(id) this.setActiveStyleSheet(id);

	$(window).unload(function() {
		var id = _this.getActiveStyleSheetId();
		if (id) {
			_this.createCookie(_this.cookieName, id, _this.cookieExpires);
		}
	});
}


/* ----------------------------------------------------- */
StyleSwitcher.prototype.getActiveStyleSheetId = function(){
	var currentId = "";
	for( var i=0; i<this.stylesheets.length; i++ ){
		if( !this.stylesheets[i].disabled ){
			currentId = this.stylesheets[i].getAttribute("id");
			return currentId;
		}
	}
	return false;
}

/* ----------------------------------------------------- */
StyleSwitcher.prototype.setActiveStyleSheet = function(id){
	for( var i=0; i<this.stylesheets.length; i++ ){
		if( this.stylesheets[i].getAttribute("id") == id ){
			if($.browser.msie){
				var _target = $('.footerNavi');
				$(_target).css('overflow','hidden');
				$(_target).css('overflow','visible');
			}
			this.createCookie( this.cookieName, id, 365 );
			this.stylesheets[i].disabled = true;
			this.stylesheets[i].disabled = false;
		}
		else{
			this.stylesheets[i].disabled = true;
		}
	}
}

/* ----------------------------------------------------- */
StyleSwitcher.prototype.createCookie = function(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

/* ----------------------------------------------------- */
StyleSwitcher.prototype.readCookie = function(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}



/* ============================================================================== */
function FontSizeSelector(options){

	this.className      = options.className;// || "fontsize";
	this.blockId        = options.blockId;// || "fontsize-selector";
	this.roi            = options.rollover;
	this.buttonsSetting = options.buttonsSetting;

	this.buttons;
	this.switcher = new StyleSwitcher(this.className);

	this.activeStyleSheetId = null;

	this.init();
}


/* ----------------------------------------------------- */
FontSizeSelector.prototype.init = function(){
	var _this = this;

	var val = this.switcher.readCookie(this.switcher.cookieName);
	if (val) {
		this.activeStyleSheetId = val;
	}
	else {
		var links = $("link");
		if (links.length) {
			links.each(function() {
				var link = $(this);
				var id =link.attr("id");
				var rel = link.attr("rel");
				var disabled = link.attr("disabled");

				if (rel && rel.match(/stylesheet/i) && id) {
					if (!rel.match(/alternate/i)) {
						_this.activeStyleSheetId = id;
					}
					else if (rel.match(/alternate/i) &&  disabled == false) {// for IE
						link.attr({"disabled": true});
					}
				}
			});
		}
	}
	if (!this.activeStyleSheetId) return;


	var sel = [];
	for (var size in this.buttonsSetting) {
		var o = this.buttonsSetting[size];
		sel.push("#" + o.buttonId);
	}
	var selector = sel.join(", ");

	this.buttons = $(selector);


	for (var size in this.buttonsSetting) {
		var o = this.buttonsSetting[size];
		var btn = $("#" + o.buttonId);
		if (btn.length) {
			if (o.styleId == this.activeStyleSheetId) {
				if (this.roi) {
					$.each(this.roi.buttons, function(index) {
						if (this.id == o.buttonId) {
							_this.roi.activate(this , _this.roi.aSuffix);
						}
					});
				}
				btn.attr({"alt": o.titleSelected, "title": o.titleSelected});
			}
			else {
				btn.attr({"alt": o.title, "title": o.title});
			}
		}
	}
}


/* ----------------------------------------------------- */
FontSizeSelector.prototype.setFontSize = function(size){
	var _this = this;
//	if (this.activeStyleSheetId == "fontsize-"+size) return;

	this.buttons.each(function() {
		var tmp = $(this).attr("id").split("-");
		var btnSize = tmp[tmp.length-1];
		////var bs = _this.buttonsSetting[size];
		var bs = _this.buttonsSetting[btnSize];

		if (btnSize && btnSize.match(size)) {
			_this.switcher.setActiveStyleSheet(bs.styleId);
			$(this).attr({"alt": bs.titleSelected, "title": bs.titleSelected});
		}
		else {
			$(this).attr({"alt": bs.title, "title" : bs.title});
		}
	});
}
